package com.hy.heath.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.heath.common.R;
import com.hy.heath.pojo.entity.HealthRecord;
import com.hy.heath.service.HealthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/health-records")
public class HealthRecordController {

    @Autowired
    private HealthRecordService healthRecordService;

    // 根据用户ID查询
    @GetMapping("/user/{userId}")
    public R<List<HealthRecord>> getByUser(@PathVariable Integer userId) {
        return R.success(healthRecordService.listByUserId(userId));
    }

    // 分页查询
    @GetMapping("/page")
    public R<Page<HealthRecord>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer userId) {
        return R.success(healthRecordService.pageRecords(
                new Page<>(pageNum, pageSize),
                userId));
    }

    // 新增记录
    @PostMapping
    public R<String> add(@RequestBody HealthRecord record) {
        healthRecordService.save(record);
        return R.success("新增成功");
    }

    // 更新记录
    @PutMapping
    public R<String> update(@RequestBody HealthRecord record) {
        healthRecordService.updateById(record);
        return R.success("更新成功");
    }

    // 删除记录
    @DeleteMapping("/{recordId}")
    public R<String> delete(@PathVariable Integer recordId) {
        healthRecordService.removeById(recordId);
        return R.success("删除成功");
    }
}